package com.zxc.controller.ceshi;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.zxc.Util.RandomValidateCode;
import com.zxc.Util.windWeather;
import com.zxc.common.Result;
import com.zxc.model.Plan;
import com.zxc.model.User;
import com.zxc.model.ceshi;
import com.zxc.service.UserService;
import org.json.JSONObject;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Controller
@CrossOrigin
@ResponseBody
@RequestMapping("/ceshi")
public class ceshiController {
    @Resource
    private UserService userService;
    //    private Logger logger = Logger.getLogger ( MainController.class );
    @RequestMapping ( value = "/check", method = RequestMethod.POST )
    public Result check (@RequestBody ceshi user) {
        System.out.println(user);
//        logger.info("MainController run");
        Result result = new Result();
        result.setData(user);
        result.setMessage("SSM vue前后端框架搭建成功");

        return result;
    }
    @RequestMapping ( value = "/check2", method = RequestMethod.POST )
    public ceshi check2 (@RequestBody ceshi user) {
        System.out.println(user.getAcct());
        System.out.println(user.getPwd());
//        logger.info("MainController run");
        Result result = new Result();
        result.setMessage("SSM vue前后端框架Json数据传输成功");
        return user;
    }

    @RequestMapping(value="/checkCode")
    public void checkCode(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //设置相应类型,告诉浏览器输出的内容为图片
        response.setContentType("image/jpeg");

        //设置响应头信息，告诉浏览器不要缓存此内容
        response.setHeader("pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expire", 0);

        RandomValidateCode randomValidateCode = new RandomValidateCode();
        try {
            randomValidateCode.getRandcode(request, response);//输出图片方法
//            System.out.println(randomValidateCode.getRandomString(1234));
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    @RequestMapping ( value = "/api", method = RequestMethod.POST )
    public  ResponseEntity<?> api () {
        int k=0;
        double tmpMax=0;
        double condCode=0;
        double pcpn=0;
        double windSpd=0;
        Plan p =new Plan();
        windWeather vo =new windWeather();
        JSONObject object = new JSONObject(vo.getWeatherApi());
        JSONObject object1 = object.getJSONArray("HeWeather6").getJSONObject(0);
        int length =object1.getJSONArray("daily_forecast").length();
        List<Plan> list= userService.getPlantList(p);

//        Array s<T> = object1.getJSONObject("daily_forecast");
        for (int i = 0; i <length; i++) {
            JSONObject s = object1.getJSONArray("daily_forecast").getJSONObject(i);
            SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd");
            Date date = null;
            Date   date2 = null;
            try {
                date=formatter.parse(s.get("date").toString());
            } catch (ParseException e) {
                e.printStackTrace();
            }
//            获取当天天气的情况
            tmpMax=Double.parseDouble(s.get("tmp_max").toString());
            condCode=Double.parseDouble(s.get("cond_code_d").toString());
            pcpn=Double.parseDouble(s.get("pcpn").toString());
            windSpd=Double.parseDouble(s.get("wind_spd").toString());
//
            for (int j = k; j <list.size(); j++) {
                try {
                    date2=formatter.parse(list.get(j).getPlanTime());
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                if(date.compareTo(date2)>0){
                        break;
                    }else {
                    list.get(j).setCondCode(vo.getCondCode(condCode));
                    list.get(i).setTemperature(vo.getTmpMax(tmpMax));
                    list.get(j).setWindSc(vo.getWindSc(windSpd));
                    list.get(i).setPcpn(vo.getPcpn(pcpn));
                    k++;
                }
            }
        }

        return ResponseEntity.ok(list);
    }
}



